package com.screenworker.compactmvc.extensions.flex.remoteobjectservice
{
	import com.screenworker.compactmvc.extensions.flex.interfaces.IRemoteObjectService;
	import com.screenworker.compactmvc.interfaces.IModel;
	import com.screenworker.compactmvc.patterns.Controller;
	import com.screenworker.compactmvc.patterns.Model;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.InvokeEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	[Bindable]
	public class RemoteObjectService extends Model implements IModel, IRemoteObjectService
	{
		public static var NAME:String = 'RemoteObjectService';

		private var _remoteObject:RemoteObject;
		
		public function RemoteObjectService(name:String, controller:Controller)
		{
			super(name, controller);
			
				
			//NET ONNECTION
			_remoteObject = new RemoteObject();
			_remoteObject.addEventListener(FaultEvent.FAULT, faultHandler, false, 0, true);
			_remoteObject.addEventListener(ResultEvent.RESULT, resultHandler, false, 0, true);
			_remoteObject.addEventListener(InvokeEvent.INVOKE, invokeHandler, false, 0, true);
		}
		

		override final public function removeHandler():void
		{
			_remoteObject.disconnect();
			_remoteObject.removeEventListener(FaultEvent.FAULT, faultHandler);
			_remoteObject.removeEventListener(ResultEvent.RESULT, resultHandler);
			_remoteObject.removeEventListener(InvokeEvent.INVOKE, invokeHandler);
			_remoteObject = null;
		}
		
		public function faultHandler(fault:FaultEvent):void
		{
			
		}
		
		public function resultHandler(result:ResultEvent):void
		{
			
		}
		
		public function invokeHandler(invoke:InvokeEvent):void
		{
			
		}
		
		/**
		 * returns a given mockup object 
		 * @param mockUp
		 * 
		 */			
		protected final function mockup(mockUp:Object):void
		{ 	
     	 	resultHandler(new ResultEvent(ResultEvent.RESULT,false, false, mockUp));
		}
		
		/**
		 * retruns the remoteObject Instance 
		 * @return 
		 * 
		 */		
		public function get remoteObject():RemoteObject
		{
			return _remoteObject;
		}
		
		/**
		 * @usage: "somyDestination (ServiceName)" 
		 * @param value
		 * 
		 */		
		public final function set destination(value:String):void
		{
			_remoteObject.destination = value;
		}
		
		public final function get destination():String
		{
			return _remoteObject.destination;
		}
		
		/**
		 * @usage: "http://www.myproject.de/myapplication/messagebroker/amf" 
		 * @param value
		 * 
		 */		
		public final function set endpoint(value:String):void
		{
			_remoteObject.endpoint = value;
		}
		
		public final function get endpoint():String
		{
			return _remoteObject.endpoint;
		}
		
		/**
		 * @usage: "my.service.namespace.MyService"; 
		 * @param value
		 * 
		 */		
		public final function set source(value:String):void
		{
			_remoteObject.source = value;
		}
		
		public final function get source():String
		{
			return _remoteObject.source;
		}
	
	}
}